//-----------------------------------------------------------------------
// <copyright file="Create.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Andy Reeves</author>
// <email>andyr</email>
// <date>2004-03-23</date>
// <summary>Creates a new Sql User.</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.Sql.Access
{
    using System;
    using System.Xml;
    using System.Globalization;
    using System.IO;
    using System.Data;
    using System.Data.SqlClient;
    using SdcSql = Microsoft.Sdc.Tasks.Configuration.SqlServer;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Grants a login access to the specified SQL server and table
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<Sql.Access.Grant UserName="userName" ServerName="serverName" DatabaseName="database" />]]></code>
    /// <para>where:</para>
    /// <para><i>UserName (Required)</i></para>
    /// <para>The login name to grant access to.</para>
    /// <para><i>DatabaseName (Required)</i></para>
    /// <para>The database on the specified server to grant access to.</para>
    /// <para><i>ConnectionString</i></para>
    /// <para>A valid connection string used to connect the SQL server to run the operation on.</para>
    /// <para><i>ServerName</i></para>
    /// <para>The server to grant access to (used instead of <see cref="SqlBase.ConnectionString"/>)</para>
    /// <para><i>ConnectionTimeout</i></para>
    /// <para>Number of seconds to wait before failing a connection attempt. 
    /// Used if a <see cref="SqlBase.ConnectionString"/> is not specified.</para>
    /// <para><i>CommandTimeout</i></para>
    /// <para>Number of seconds to wait before failing the grant request. </para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Sql.Access.Grant
    ///             UserName="testUser"
    ///             ServerName="(local)"
    ///             DatabaseName="testDB" />
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>

    public class Grant : SqlBase
    {
        string userName, databaseName;

        /// <summary>
        /// Initializes a new instance of the CreateSqlUser class.
        /// </summary>
        public Grant()
        {
            // Because this operation doesn't had to run against a database
            // default the database to master so the connection string
            // can build properly
            this.DatabaseName = "master";
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            #region Execute code
            Log.LogMessage("Checking for existance " + this.ConnectionString);
            if (!SdcSql.Login.ExistsWithConnectionString(userName, this.ConnectionString))
            {
                Log.LogMessageFromResources("Sql.CreateLogin", userName);
                SdcSql.Login.CreateWithConnectionString(userName, this.ConnectionString);
            }

            Log.LogMessage("Granting access");
            if (!SdcSql.User.ExistsWithConnectionString(userName, this.DatabaseName, this.ConnectionString))
            {
                Log.LogMessageFromResources("Sql.CreateUser", userName, this.DatabaseName);
                SdcSql.User.GrantDBAccessWithConnectionString(userName, this.DatabaseName, userName, this.ConnectionString);
            }
            
            #endregion
        }


        /// <summary>
        /// Login account to grant access to
        /// </summary>
        /// <value>A valid login account, in the form machine\username</value>
        [Required]
        public string UserName
        {
            get
            {
                return (this.userName == null ? String.Empty : this.userName);
            }
            set
            {
                if (value == null)
                {
                    throw (new ArgumentNullException("UserName", "UserName cannot be null"));
                }
                else if (value.Length == 0)
                {
                    throw (new ArgumentOutOfRangeException("UserName", "UserName cannot be an empty string"));
                }
                else
                {
                    this.userName = value;
                }
            }
        }

        /// <summary>
        /// Name of the database that the user will be granted access to
        /// </summary>
        /// <value>A valid database name for the specified server</value>
        [Required]
        public string DatabaseName
        {
            get
            {
                return (this.databaseName);
            }
            set
            {
              this.databaseName = value;
            }
        }
    }
}